package com.thkj.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 *
 * @author hwt
 */


public class DateUtils {

    /**
     * 获取当前年月日时分秒
     *
     * @return
     */
    public static String getToday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        String today = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return today;
    }

    /**
     * 获取当前年月日时分秒
     *
     * @return
     */
    public static String getTodayTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        String today = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());
        return today;
    }

    /**
     * 获取当前年月
     *
     * @return
     */
    public static String getMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        String today = new SimpleDateFormat("yyyy-MM").format(cal.getTime());
        return today;
    }


    /**
     * 获取现在时间
     *
     * @return
     */
    public static String getTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        String today = new SimpleDateFormat("HH:mm:ss").format(cal.getTime());
        return today;
    }

    /**
     * 获取订单号
     *
     * @return
     */
    public static String getOrderNo() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        String today = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(cal.getTime());
        return today;
    }


    /**
     * 将日期转换成时间戳
     *
     * @return
     */
    public static long getDateLong(String dates) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return df.parse(dates).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 将时间戳转换成日期
     */
    public static String dateToString(Date data, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String dates = sdf.format(data);
        return dates;
    }

    /**
     * 将时间戳转换成日期
     *
     * @param timeStamp
     * @return
     */
    public static String longToDate(String timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dates = sdf.format(new Date(Long.parseLong(timeStamp)));
        return dates;
    }

    /**
     * 将时间转换成时间戳
     *
     * @param times
     * @return
     */
    public static long getTimeLong(String times) {
        DateFormat df = new SimpleDateFormat("hh:mm:ss");
        try {
            return df.parse(times).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 将时间戳转换成时间
     *
     * @param timeStamp
     * @return
     */
    public static String longToTime(String timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        String dates = sdf.format(new Date(Long.parseLong(timeStamp)));
        return dates;
    }

    /**
     * 与当前时间比较大小, 比当前时间大返回true，否则返回false
     */
    public static boolean compareBigCureDateTime(String dateTime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return df.parse(dateTime).after(new Date());
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 增加天数
     *
     * @param source
     * @param dayNum
     * @return
     */
    public static String addDay(Date source, int dayNum) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(source);
        cal.add(Calendar.DATE, dayNum);
        return df.format(cal.getTime());
    }

    /**
     * 增加天数
     *
     * @param source
     * @param dayNum
     * @return
     */
    public static String addDay(String source, int dayNum) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(df.parse(source));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        cal.add(Calendar.DATE, dayNum);
        return df.format(cal.getTime());
    }

    /**
     * 增加月份
     *
     * @param source
     * @param mohthNum
     * @return
     */
    public static Date addMonth(Date source, int mohthNum) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(source);
        cal.add(Calendar.MONTH, mohthNum);
        return cal.getTime();
    }

    /**
     * 增加年份
     *
     * @param source
     * @param yearNum
     * @return
     */
    public static String addYear(Date source, int yearNum) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(source);
        cal.add(Calendar.YEAR, yearNum);
        return df.format(cal.getTime());
    }

    /**
     * 增加月份
     *
     * @param source
     * @param mohthNum
     * @return
     */
    public static String addMonth(String source, int mohthNum, String format) {
        DateFormat df = new SimpleDateFormat(format);
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(df.parse(source));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        cal.add(Calendar.MONTH, mohthNum);
        return df.format(cal.getTime());
    }

    /**
     * 增加月份
     *
     * @param source
     * @param mohthNum
     * @param format
     * @return
     */
    public static String addMonth(Date source, int mohthNum, String format) {
        DateFormat df = new SimpleDateFormat(format);
        Calendar cal = Calendar.getInstance();
        cal.setTime(source);
        cal.add(Calendar.MONTH, mohthNum);
        return df.format(cal.getTime());
    }

    /**
     * 增加月份(Date)
     *
     * @param source
     * @param mohthNum
     * @return
     */
    public static Date addMonthForDate(Date source, int mohthNum) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(source);
        cal.add(Calendar.MONTH, mohthNum);
        return cal.getTime();
    }

    /**
     * 增加年份
     *
     * @param source
     * @param yearNum
     * @return
     */
    public static String addYear(String source, int yearNum) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(df.parse(source));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        cal.add(Calendar.YEAR, yearNum);
        return df.format(cal.getTime());
    }

    /**
     * 比较当前时间与系统时间大小（精确到分钟）
     *
     * @param times
     * @return
     */
    public static boolean sizeComparison(String times) {
        String now[] = getTime().split(":");
        String t[] = times.split(":");
        if (Integer.parseInt(now[0]) * 60 + Integer.parseInt(now[1]) > Integer.parseInt(t[0]) * 60 + Integer.parseInt(t[1])) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 获取当月天数
     *
     * @return
     */
    public static int getDayOfMonth() {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.set(Calendar.DAY_OF_MONTH, 1);
        aCalendar.add(Calendar.DAY_OF_MONTH, -1);
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        return day;
    }

    /**
     * 根基月过去当前的天数
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取当前年月日时分秒
     *
     * @return
     */
    public static String getTodayFormatSpe() {
        Calendar cal = Calendar.getInstance();
        String today = new SimpleDateFormat("yyyy年MM月dd日").format(cal.getTime());
        return today;
    }


    /**
     * 获取从当天到指定天数后的日期列表(包括今天)
     *
     * @param dayNum
     */
    public static String[] getDateListByDayCount(int dayNum) {
        SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
        String[] days = new String[dayNum + 1];
        for (int i = 0; i < dayNum + 1; i++) {
            Calendar aCalendar = Calendar.getInstance();
            aCalendar.add(Calendar.DATE, i);
            days[i] = dFormat.format(aCalendar.getTime());
        }
        return days;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static Date stringToDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 格式转换
     *
     * @param date
     * @return
     */
    public static String formatWxPayTime(String date) {
        SimpleDateFormat dd1 = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dd.format(dd1.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static void main(String[] args) {
        System.out.println(getOrderNo());
        System.out.println(getOrderNo());
    }
}
